package com.xidian.zhiancloud.controller.luling.PointPart;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.xidian.zhiancloud.dao.EquipFacStatusMapper;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.EquipFacStatus;
import com.xidian.zhiancloud.model.EquipFacStatusExample;
import com.xidian.zhiancloud.service.luling.PointPart.EquipFacStatusService;
import com.xidian.zhiancloud.util.tools.RequestUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 ** 设备设施清单接口
 * @Author:luling
 * @Date:2021/1/7   15:24
 */
@RestController
@RequestMapping("/EquipFacStatus")
public class EquipFacStatusController {
    @Autowired
    private EquipFacStatusService equipFacStatusService;
    private EquipFacStatusExample input;
    /**
   *添加设施设备信息
     @param   equipFacStatus 设施设备对象
     @return ResultDTO 对象
     */
    @PostMapping("/insert")
    public ResultDTO<Boolean> insert(@RequestBody EquipFacStatus equipFacStatus) {
        equipFacStatusService.insert(equipFacStatus);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
    /**
     * 删除设施设备信息
     * @param  equipFacStatus  删除设施设备对象
     * @return ResultDTO对象
     */
    @PostMapping("/delete")
    public ResultDTO<Boolean> delete(@RequestBody EquipFacStatus equipFacStatus) {
        equipFacStatusService.delete(equipFacStatus);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
    /**
     * 修改设施设备信息
     * @param  equipFacStatus 修改设施设备对象
     * @return ResultDTO对象
     */
    @PostMapping("/update")
    public ResultDTO<Boolean> update(@RequestBody EquipFacStatus equipFacStatus) {
        equipFacStatusService.update(equipFacStatus);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
    /**
     * 根据id删除设施设备
     *
     * @param ids ids数组
     * @return  返回ResultDTO对象*/
    @DeleteMapping("/deleteByIds")
    public ResultDTO deleteByIds(Long[] ids) {
        equipFacStatusService.deleteByIds(ids);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
    /**
     * 根据equipType获取设施设备信息
     *
     * @param equipType 设施设备对象的equipType
     * @return  返回设施设备对象
     */
    @GetMapping("/getById/{equipType}")
    public ResultDTO<Object> getById(
            @PathVariable String equipType) {
        return ResultDTO.successOf(equipFacStatusService.getById(equipType), GeneralResponseEnums.SUCCESS);
    }
//   /**
//    * 列表查询
//    *
//    * @param  equipName 设备名称
//    * @param mode 规格/型号
//    * @param  addBy  添加人
//    * @param   key    关键字
//    * @param current   分页偏移量
//    * @param size    每一页的数量
//    * @return 返回设施设备列表
//    **/
//    @GetMapping("/getList")
//    public ResultDTO<List<EquipFacStatus>> selectequipfacstatusList(
//                             @RequestParam(value = "equipName", required = false) String equipName,
//                             @RequestParam(value = "mode", required = false) String mode,
//                             @RequestParam(value = "addBy", required = false) String addBy,
//                             @RequestParam(value = "key", required = false) String key,
//                             @RequestParam(value = "current", required = true) int current,
//                             @RequestParam(value = "size", required = true) int size) {
//        return ResultDTO.successOf(equipFacStatusService.getList(equipName, mode, addBy,key,current, size), GeneralResponseEnums.SUCCESS);
//    }

         /* 将设施设备导出到excel文件中去
        *
        * @param * 设施设备信息列表
         * @param * 输出表
         * @throws IOException
         */
        //TODO 待测试
       @RequestMapping("exportEquipFacEx")
      public void exportEquipFac(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {

           String orgi = "EquipFac";
           String id = RequestUtil.getStringParameter(request, "id", "");//ID
            String companyId = RequestUtil.getStringParameter(request, "companyId", "");
            String riskNum = RequestUtil.getStringParameter(request, "riskNum", "");
            String equipNum = RequestUtil.getStringParameter(request, "equipNum", "");
           String equipName = RequestUtil.getStringParameter(request, "equipName", "");
           String equipType = RequestUtil.getStringParameter(request, "equipType", "");
           String mode = RequestUtil.getStringParameter(request, "mode", "");
           String position = RequestUtil.getStringParameter(request, "position", "");
           String administer = RequestUtil.getStringParameter(request, "administer", "");
           String equipSp = RequestUtil.getStringParameter(request, "equipSp", "");
           String addBy = RequestUtil.getStringParameter(request, "addBy", "");
           String delStatus = RequestUtil.getStringParameter(request, "addBy", "");
           String createTime = RequestUtil.getStringParameter(request, "createTime", "");
           String updateTime = RequestUtil.getStringParameter(request, "updateTime", "");

           Map<String, Object> mapResult = new HashMap<String, Object>();mapResult.put("id", id);  mapResult.put("companyId", companyId);
        mapResult.put("companyId", riskNum);mapResult.put("equipNum", equipNum);
        mapResult.put("equipType", equipType);mapResult.put("equipName", equipName);
        mapResult.put("mode", mode);mapResult.put("position", position);
        mapResult.put("administer", administer);mapResult.put("equipSp", equipSp);
        mapResult.put("addBy", addBy);mapResult.put("addBy", addBy);
        mapResult.put("createTime", createTime);mapResult.put("updateTime", updateTime);

           List<EquipFacStatus> EquipFacList = null;

           if (    StringUtils.isNotEmpty(id) || StringUtils.isNotEmpty(companyId) ||
                   StringUtils.isNotEmpty(companyId) || StringUtils.isNotEmpty(equipNum) ||
                   StringUtils.isNotEmpty(equipType) || StringUtils.isNotEmpty(equipName) ||
                   StringUtils.isNotEmpty(mode) ||StringUtils.isNotEmpty(position)||StringUtils.isNotEmpty(administer)||
                   StringUtils.isNotEmpty(equipSp)||StringUtils.isNotEmpty(addBy)||StringUtils.isNotEmpty(addBy)||
                   StringUtils.isNotEmpty(createTime)||StringUtils.isNotEmpty(updateTime)) {
             //  EquipFacList= (List<EquipFacStatus>) EquipFacStatusService.selectByExample(example);
           }
        // TODO 待这个方法待商榷
        // else if(
           //          StringUtils.isEmpty(id) && StringUtils.isEmpty(departmentName) &&
    //               StringUtils.isEmpty(organizationType) && StringUtils.isEmpty(isSafeDepartment)
    ////                && StringUtils.isEmpty(principalId) && StringUtils.isEmpty(codes) &&
    ////                StringUtils.isEmpty(phoneNum)) {
    ////        organizationList = organizationService.findByOrgiOrderByCreatetimeDesc(orgi);
    ////        }
    //
    response.reset(); // 必要地清除response中的缓存信息

           response.setCharacterEncoding("utf-8");
            response.setContentType("EquipFac/vnd.ms-excel");
            String fileName = "设施设备清单表.xls";

            if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
                fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
                // firefox浏览器
           } else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
                fileName = URLEncoder.encode(fileName, "UTF-8");// IE浏览器
            } else if (request.getHeader("User-Agent").toUpperCase().indexOf("CHROME") > 0) {
                fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
               // 谷歌
            }

           response.setHeader("Content-Disposition", "attachment; filename=" + fileName);

            //07版本以前的Excel文件,即后缀名为.xls,
           HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("EquipFacList");
            HSSFRow row;
            HSSFCell cell;
            String createtime = "";
            String updatetime = "";
            row = sheet.createRow(0);
            String[] titles = {"序号", "风险点编号", "设施设备编号", "设施设备名称 ", "设施设备类型"};
            for (int i = 0; i < titles.length; i++) { // 创建一个单元格，设置单元格的值
                cell = row.createCell(i);
                cell.setCellValue(titles[i]);
            }
            // 从第1行开始写数据
            for (int i = 0; i < EquipFacList.size(); i++) {
                // 创建表格的第i+1行
                row = sheet.createRow(i + 1);
                // 写该行的各列数据
                EquipFacStatus EquipFac = EquipFacList.get(i);
                int j = 0;

                Long Id = Long.valueOf(EquipFac.getId());//公司机构类型
                String RiskNum = String.valueOf(EquipFac.getRiskNum());//是否是安全机构

                cell = row.createCell(j++);
                cell.setCellValue(i + 1); // 序号
                cell = row.createCell(j++);
                cell.setCellValue(EquipFac.getId());//部门名称
               /** cell = row.createCell(j++);
                cell.setCellValue
                        (Id.equals("1") ? "公司级" :
                                (Id.equals("2")) ? "部门级" :
                                        (Id.equals("3")) ? "车间级" :
                                                (Id.equals("4")) ? "班组级" : null);//公司机构类型**/
                cell = row.createCell(j++);
                cell.setCellValue(EquipFac.getId());
                cell = row.createCell(j++);
                cell.setCellValue(EquipFac.getRiskNum());
                cell = row.createCell(j++);
                cell.setCellValue(EquipFac.getEquipName());
                cell = row.createCell(j++);
                cell.setCellValue(EquipFac.getEquipType());
                cell = row.createCell(j++);
                cell.setCellValue(EquipFac.getEquipNum());

            }

            // 将excel的数据写入文件
            ServletOutputStream os = response.getOutputStream();
            wb.write(os);
            os.close();

        }
}


